<!DOCTYPE html>
<html>
<head>
	<title>Add/remove items with new column</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/fonts/font_roboto/roboto.css"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/dhtmlx.css"/>
	<script src="../../../codebase/dhtmlx.js"></script>
	<script>
		var myForm, formData;
		function doOnLoad() {
			formData = [
				{type: "settings", position: "label-left", labelWidth: "auto", inputWidth: "auto"},
				{type: "label", label: "Query Options", list: [
					{type: "settings", position: "label-right"},
					{type: "checkbox", name: "ch_where", value: 1, label: "WHERE"},
					{type: "checkbox", name: "ch_order", value: 1, label: "ORDER BY"}
				]},
				{type: "label", label: "Query Builder", name: "query", offsetTop: 20, list: [
					{type: "settings", offsetLeft: 5},
					{type: "select", label: "SELECT", inputWidth: 100, userdata: {output: true, order: 10, before: "SELECT ", after: " "}, options:[
						{value: "*", text: "*", selected: true},
						{value: "userId", text: "userId"},
						{value: "userName", text: "userName"}
					]},
					{type: "newcolumn"},
					{type: "select", label: "FROM", inputWidth: 70, userdata: {output: true, order: 20, before: "FROM ", after: "\n"}, options: [
						{value: "users", text: "users", selected: true}
					]}
				]},
				{type: "label", label: "Query String", offsetTop: 20, list: [
					{type: "input", name: "query_string", label: "", inputWidth: 300, rows: 4}
				]}
			];
			myForm = new dhtmlXForm("myForm", formData);
			myForm.attachEvent("onChange", function(name, value, state){
				if (name == "ch_where" || name == "ch_order") {
					var itemOffset = (myForm.isItemChecked("ch_where") && name == "ch_order" ? 3:0); // include "where" position for "order_by"
					var lastItemIndex = 1+itemOffset; // index of last item in query ([0] for "select", [1] for "from")
					for (var q=0; q<itemData[name].length; q++) {
						if (state) {
							myForm.addItem("query", itemData[name][q], lastItemIndex+q, true);
							myForm.addItem("query", {type: "newcolumn"}, lastItemIndex+1+q);
						} else {
							myForm.removeColumn("query", lastItemIndex+1, true);
						}
					}
				}
				updateQueryString();
			});
			updateQueryString();
		}
		function updateQueryString() {
			var out = [];
			var t = myForm.getFormData();
			for (var a in t) {
				if (myForm.getUserData(a,"output")) {
					out.push([myForm.getUserData(a,"order"), myForm.getUserData(a,"before")+t[a].replace(/\\/,"\\\\").replace(/\'/gi,"\\'")+myForm.getUserData(a,"after")]);
				}
			}
			out.sort(function(a,b){return (a[0]>=b[0]?1:-1);});
			for (var q=0; q<out.length; q++) out[q] = out[q][1];
			myForm.setItemValue("query_string", out.join(""));
		}
		// formData for additional items
		var itemData = {
			"ch_where": [
				{type: "select", label: "WHERE", inputWidth: 100, userdata: {output: true, order: 30, before: "WHERE ", after: " "}, options: [
					{value: "userId", text: "userId"},
					{value: "userName", text: "userName"}
				]},
				{type: "select", label: " ", inputWidth: 60, userdata: {output: true, order: 31, after: " "}, options: [
					{value: "LIKE", text: "LIKE"},
					{value: "=", text: "=", selected: true},
					{value: ">", text: ">"},
					{value: ">=", text: ">="},
					{value: "<", text: "<"},
					{value: "<=", text: "<="}
				]},
				{type: "input", label: " ", value: "1", userdata: {output: true, order: 32, before: "'", after: "'\n"}, inputWidth: 50}
			],
			"ch_order": [
				{type: "select", label: "ORDER BY", inputWidth: 100, userdata: {output: true, order: 40, before: "ORDER BY ", after: " "}, options: [
					{value: "userId", text: "userId"},
					{value: "userName", text: "userName", selected: true}
				]},
				{type: "select", label: " ", inputWidth: 65, userdata: {output: true, order: 41}, options: [
					{value: "ASC", text: "ASC", selected: true},
					{value: "DESC", text: "DESC"}
				]}
			]
		};
	</script>
</head>
<body onload="doOnLoad();">
	<div id="myForm"></div>
</body>
</html>